<?php

/**
 * 上传控制器
 */
class UploadController extends \Our\Controller_AbstractAdmin
{
    /**
     * 上传控制器
     */
    public function indexAction()
    {
        try {
            $i = $this->_getPost('i', 0); //该参数用于识别是哪个input上传的图片

            $imageSize = 20000;
            //上传到本地服务器
            $uploadConfig = new \Our\Upload\Config(array(
                    'dir' => '/upload/tmp',
                    'fileName' => 'upload_image_file',
                    'saveFilename' => md5(time() . rand(10000, 99999)),
                    'size' => $imageSize * 1024)
            );
            $upload = new \Our\Upload\Main($uploadConfig);
            $result = $upload->upload();

            $imageFile = rtrim($result[1], '/') . '/' . $result[2]; //本地服务器保存的图片，存在放/public/image目录下
            //上传到云服务器
            $cloudMethod = '_' . Bootstrap::getConfig("upload.cloud.type") . "Upload";
            $uploadCloudResult = $this->$cloudMethod($imageFile);

            $imageSize = getimagesize($imageFile);
            $data = array(
                'width' => $imageSize[0],
                'height' => $imageSize[1],
                'dest_file_name' => Bootstrap::getConfig("upload.pic.url") . $uploadCloudResult['file_relative_url'],
                'status' => 1,
            );

            //上传到云服务器后，删除本地文件
            if (file_exists($imageFile)) {
                unlink($imageFile);
            }

            $this->getView()->assign('i', $i);
            $this->getView()->assign('data', json_encode($data));
        } catch (Exception $e) {
            $this->getView()->assign('i', $i);
            $this->getView()->assign('data', json_encode(array()));
            $this->getView()->assign('errmsg', $e->getMessage());
        }
    }

    /**
     * 多图片上传接口
     */
    public function mulitpicsAction()
    {
        \Yaf\Dispatcher::getInstance()->disableView();
        $faildata = array(
            'dest_file_name' => '',
            'status' => 0,
            'resource_host' => \Bootstrap::getConfigUrl('resourcesHost')
        );
        try {
            //上传到本地服务器
            $uploadConfig = new \Our\Upload\Config(array(
                    'dir' => '/upload/tmp',
                    'fileName' => 'upload_image_file',
                    'saveFilename' => md5(time() . rand(10000, 99999)))
            );
            $upload = new \Our\Upload\Main($uploadConfig);
            $result = $upload->upload();
            $imageFile = rtrim($result[1], '/') . '/' . $result[2]; //本地服务器保存的图片，存在放/public/image目录下
            //上传到云服务器
            $cloudMethod = '_' . Bootstrap::getConfig("upload.cloud.type") . "Upload";
            $cuResult = $this->$cloudMethod($imageFile);

            $data = array(
                'dest_file_name' => Bootstrap::getConfig("upload.pic.url") . $cuResult['file_relative_url'],
                'status' => 1,
                'resource_host' => Bootstrap::getConfig("upload.pic.url")
            );
            if (file_exists($imageFile)) {
                unlink($imageFile); //上传到又拍云后删除本地服务器图片
            }
            die(json_encode($data));
        } catch (Exception $e) {
            die(json_encode($faildata));
        }
    }

}
